<?php
/**
 *
 */
class Parser
{
  const NAME = "Spielerinfo von ";
  const ALLIANCE = "Allianz";
  const META_ALLIANCE = "Metaallianz";
  const RESEARCH = "Forschung";
  const TOTAL = "gesamt";

  public static function parsePublicInfo($rawText)
  {
    $publicInfo = array();
    $planetCnt = 0;
    $rows = explode("\n", $rawText);
    foreach ($rows as $row => $data) {
      // nur nicht leere Zeilen
      if (trim($data) == true) {
        // player name
        if (strpos($data, self::NAME) === 0) {
          $publicInfo['player'] = trim(substr($data, strlen(self::NAME)));
        }
        // players alliance
        if (strpos($data, self::ALLIANCE) === 0) {
          if (is_string(trim(substr($data, strlen(self::ALLIANCE)))))
            $publicInfo['ally'] = trim(substr($data, strlen(self::ALLIANCE)));
          $tmpPos1 = strpos($publicInfo['ally'], "[");
          $tmpPos2 = strpos($publicInfo['ally'], "]") - 1;
          if (is_string(trim(substr($publicInfo['ally'], 0, $tmpPos1))))
            $publicInfo['ally_long'] = trim(substr($publicInfo['ally'], 0, $tmpPos1));
          if (is_string(trim(substr($publicInfo['ally'], $tmpPos1 + 1, $tmpPos2 - $tmpPos1))))
            $publicInfo['ally_short'] = trim(substr($publicInfo['ally'], $tmpPos1 + 1, $tmpPos2 - $tmpPos1));
        }
        // players meta
        if (strpos($data, self::META_ALLIANCE) === 0) {
          if (is_string(trim(substr($data, strlen(self::META_ALLIANCE)))))
            $publicInfo['meta'] = trim(substr($data, strlen(self::META_ALLIANCE)));
          $tmpPos1 = strpos($publicInfo['meta'], "[");
          $tmpPos2 = strpos($publicInfo['meta'], "]") - 1;
          if (is_string(trim(substr($publicInfo['meta'], 0, $tmpPos1))))
            $publicInfo['meta_long'] = trim(substr($publicInfo['meta'], 0, $tmpPos1));
          if (is_string(trim(substr($publicInfo['meta'], $tmpPos1 + 1, $tmpPos2 - $tmpPos1))))
            $publicInfo['meta_short'] = trim(substr($publicInfo['meta'], $tmpPos1 + 1, $tmpPos2 - $tmpPos1));
        }
        // player planets
        if (preg_match('/\(33([3|4]{1}):([1-9]{3}):([A-Z]{1})/', $data)) {
          $tmpPos1 = strpos($data, "(");
          $tmpPos2 = strpos($data, ")") - 1;
          if (is_string(trim(substr($data, 0, $tmpPos1))))
            $publicInfo['planets'][$planetCnt]['name'] = trim(substr($data, 0, $tmpPos1));
          $publicInfo['planets'][$planetCnt]['coord'] = trim(substr($data, $tmpPos1 + 1, $tmpPos2 - $tmpPos1));
          if (is_numeric(trim(substr($data, $tmpPos2 + 2))))
            $publicInfo['planets'][$planetCnt]['points'] = trim(substr($data, $tmpPos2 + 2));

          $coords = explode(':', $publicInfo['planets'][$planetCnt]['coord']);
          if (is_numeric($coords[0]))
            $publicInfo['planets'][$planetCnt]['coord_galaxy'] = $coords[0];
          if (is_numeric($coords[1]))
            $publicInfo['planets'][$planetCnt]['coord_solar'] = $coords[1];
          if (is_string($coords[2]))
            $publicInfo['planets'][$planetCnt]['coord_position'] = self::planetChrToPos($coords[2]);
          //echo "@".$publicInfo['planet'][$planetCnt]['name']."@";
          //echo "@".$publicInfo['planet'][$planetCnt]['coord']."@";
          //echo "@".$publicInfo['planet'][$planetCnt]['points']."@";
          //CVarDumper::dump($coords);
          $planetCnt++;
        }
        // players research points
        if (strpos($data, self::RESEARCH) === 0) {
          if (is_numeric(trim($rows[$row + 1]))) {
            $publicInfo['research'] = trim($rows[$row + 1]);
          }
        }
        // players total points
        if (strpos($data, self::TOTAL) === 0) {
          if (is_numeric(trim($rows[$row + 1])))
            $publicInfo['total'] = trim($rows[$row + 1]);
        }
        //echo $row.' - '.$data.'<br/>';
      }
    } // foreach
    if (empty($publicInfo['ally'])) {
      $publicInfo['ally'] = NULL;
      $publicInfo['ally_long'] = NULL;
      $publicInfo['ally_short'] = NULL;
    }
    if (empty($publicInfo['meta'])) {
      $publicInfo['meta'] = NULL;
      $publicInfo['meta_long'] = NULL;
      $publicInfo['meta_short'] = NULL;
    }
    //CVarDumper::dump($publicInfo);
    return $publicInfo;
  }

  public static function planetChrToPos($char) {
    return ord($char)-64;
  }

  public static function  planetPosToChr($pos) {
    return chr($pos+64);
  }

}